package com.example.chessapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TempActivity extends AppCompatActivity {
    EditText input;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_temp);
        input = findViewById(R.id.input);
        Button bt_1 = findViewById(R.id.convertCtoF);
        Button bt_2 = findViewById(R.id.convertFtoC);
    }

    public void clickTemp(View v){
        if(input.getText().toString() != null){
            if(v.getId()==R.id.convertCtoF){
                String str = input.getText().toString();
                Double tempC = Double.valueOf(str);
                Double tempF = tempC*(1.8)+32;
                String s = tempF.toString();
                TextView tt = findViewById(R.id.output);
                tt.setText(s);
                input.clearFocus();
            }else{
                String str = input.getText().toString();
                Double tempF = Double.valueOf(str);
                Double tempC = (tempF-32)/1.8;
                String s = tempC.toString();
                TextView tt = findViewById(R.id.output);
                tt.setText(s);
                input.clearFocus();
            }
        }else{
            Toast.makeText(this,"请输入温度",Toast.LENGTH_LONG).show();
        }
    }

    public void Tempback(View V){
        Intent intent=new Intent(TempActivity.this,MainActivity.class);
        startActivity(intent);
    }
}